package top.abilities.util.utils.Excel;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Auhtor: ciro
 * @Description
 * @Date: 2018/1/5 17:36
 * @Modified By:
 */
public class TransType {
    public static Object translate(Class srcClass,Class destClass,Object inObj){
        String src=getType(srcClass);
        String dest=getType(destClass);
        String object =inObj.toString();
        if(src.equals(dest)){
            if(StringUtils.isEmpty(object))
                return null;
            return inObj;
        }
        if(src.equals("double")&&dest.equals("integer")){
            Double d=Double.parseDouble(object);
            return d.intValue();
        }
        if(src.equals("double")&&dest.equals("long")){
            return Long.getLong(object);
        }
        if(src.equals("double")&&dest.equals("bigdecimal")){
            return new BigDecimal(object);
        }
        if(src.equals("long")&&dest.equals("double")){
            Long l=(Long)inObj;
            return((Long) inObj).doubleValue();
        }

        if(src.equals("double")&&dest.equals("string")){
            return object;
        }
        if(src.equals("string")&&dest.equals("integer")){
            if (StringUtils.isEmpty(object))
                return null;
            return Integer.valueOf(object);
        }
        if(src.equals("string")&&dest.equals("int")){
            if (StringUtils.isEmpty(object))
                return null;
            return Integer.parseInt(object);
        }
        if(src.equals("string")&&dest.equals("double")){
            return Double.parseDouble(object);
        }
        if(src.equals("string")&&dest.equals("long")){
            return Long.parseLong(object);
        }
        if(src.equals("string")&&dest.equals("bigdecimal")){
            if(StringUtils.isEmpty(object))
                return null;
            else
                return new BigDecimal(object);

        }
        if(src.equals("string")&&dest.equals("date")){
            if (StringUtils.isEmpty(object))
                return null;
            return new Date(object);
        }
        return object;
    }
    public static String getType(Class srcClass){
        String type= srcClass.getTypeName();
        type=type.substring(type.lastIndexOf(".")+1).toLowerCase();
        return type;
    }
}
